package com.hnyc.controller;

import com.hnyc.domain.AjaxResult;
import com.hnyc.domain.CandConstants;
import com.hnyc.domain.CandRole;
import com.hnyc.domain.RoleCountUser;
import com.hnyc.service.ICandRoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: XieYingHao
 * @date: 2023/7/1 11:00
 */
@RestController
@RequestMapping("/cand/role")
public class CandRoleController {

    @Resource
    private ICandRoleService candRoleService;

    /**
     * 新增角色
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CandRole role) {
        if (CandConstants.NOT_UNIQUE.equals(candRoleService.checkRoleNameUnique(role))) {
            return AjaxResult.error("新增用户'" + role.getRoleName() + "'失败，登录账号已存在");
        }
        return candRoleService.insertRole(role) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改角色
     */
    @PutMapping
    public AjaxResult edit( @RequestBody CandRole role) {

        if (CandConstants.NOT_UNIQUE.equals(candRoleService.checkRoleNameUnique(role))) {
            return AjaxResult.error("修改用户'" + role.getRoleName() + "'失败，账号已存在");
        }
        return candRoleService.updateRole(role) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 获取角色列表
     */
    @GetMapping("/list")
    public AjaxResult list(CandRole role) {
        return AjaxResult.success(candRoleService.selectRoleList(role));
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{roleId}")
    public AjaxResult remove(@PathVariable Long roleId)
    {
        return candRoleService.deleteRoleById(roleId) > 0 ? AjaxResult.success() : AjaxResult.error();
    }


}
